package cn.fz.relaxation.job.csdn;

import cn.fz.relaxation.dao.entity.RealxHotSearchDO;
import cn.fz.relaxation.job.BaseHotSearchJob;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static cn.fz.relaxation.common.enums.HotSearchEnum.CSDN;

@Slf4j
@Component
public class CsdnHotSearchJob extends BaseHotSearchJob {

    @Override
    @XxlJob("csdnHotSearchJob")
    protected ReturnT<String> hotSearch(String param) throws IOException {
        log.info("Csdn热搜爬虫任务开始");
        this.resourceCode = CSDN.getCode();
        try {
            // 查询CSDN热搜数据
            OkHttpClient client = new OkHttpClient().newBuilder().build();
            Request request = new Request.Builder().url(
                    "https://blog.csdn.net/phoenix/web/blog/hot-rank?page=0&pageSize=25").addHeader("User-Agent",
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0"
                            + ".3770.142 Safari/537.36").method("GET", null).build();
            Response response = client.newCall(request).execute();
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            JSONArray array = jsonObject.getJSONArray("data");
            for (int i = 0, len = array.size(); i < len; i++) {
                // 获取知乎热搜信息
                JSONObject object = (JSONObject)array.get(i);
                // 构建热搜信息榜
                RealxHotSearchDO searchDO = RealxHotSearchDO.builder().hotSearchResource(resourceCode).build();
                // 设置知乎三方ID
                searchDO.setHotSearchId(object.getString("productId"));
                // 设置文章标题
                searchDO.setHotSearchTitle(object.getString("articleTitle"));
                // 设置文章连接
                searchDO.setHotSearchUrl(object.getString("articleDetailUrl"));
                // 设置热搜热度
                searchDO.setHotSearchHeat(object.getString("hotRankScore"));
                // 设置热搜作者
                searchDO.setHotSearchAuthor(object.getString("nickName"));
                // 设置热搜作者头像
                searchDO.setHotSearchAuthorAvatar(object.getString("avatarUrl"));
                // 按顺序排名
                searchDO.setHotSearchOrder(i + 1);
                // 添加到集合
                searchDOList.add(searchDO);
            }
            if (CollectionUtils.isEmpty(searchDOList)){
                log.error("没有获取到Csdn数据");
                return ReturnT.FAIL;
            }
            // 数据加到缓存和数据持久化
            this.saveSearchListToDBAndMap();
            log.info("Csdn热搜爬虫任务结束");
        } catch (IOException e) {
            log.error("获取Csdn数据异常", e);
        }
        return ReturnT.SUCCESS;
    }
}
